package com.itheima.web;

import com.itheima.Uservice.UserService;
import com.itheima.pojo.User;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.beans.beancontext.BeanContext;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet(name = "Servletdemo", value = "/loginServlet")
public class Servletdemo extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }



    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        //根据浏览器请求的数据通过其中的键获取传递的参数
        Map<String, String[]> parameterMap = request.getParameterMap();

        //通过方法将获取的map数据封装到user对象中
        User user = new User();
        try {
            BeanUtils.populate(user , parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        UserService userService = new UserService();
        boolean login = userService.login(user);

        if (login){
            request.getRequestDispatcher("/success.html").forward(request,response);
        }else {
            request.getRequestDispatcher("/error.html").forward(request,response);
        }


    }


}
